<?php
	class TEvenement extends SQLite3 {
		private $i_idEvenement;
		private $n_dateEvenement;
		private $s_periodeEvenement;
		private $b_isLogement;
		private $s_descriptionEvenement;
		private $i_idMembre;


		public function __construct()   {
                	if(file_exists('tacks.db')) {
                        	$this->open('tacks.db');
                	}
        	}
 		public static function getListeEvenementsLogement() {
                	header('Content-type: application/json');
                	$db = new TEvenement();
                	$a_result = $db->query("SELECT PRENOM,DESCRIPTION_EVENEMENT,DATE_EVENEMENT,HEURE_EVENEMENT,PERIODE_EVENEMENT 
						From EVENEMENT AS E JOIN MEMBRE AS M ON E.ID_MEMBRE=M.ID_MEMBRE
						WHERE IS_LOGEMENT = 1
						ORDER BY DATE_EVENEMENT DESC;"
						);

                	$a_evenements = array();
			if(!empty($a_result)) {
                		while($res = $a_result->fetchArray(SQLITE3_ASSOC)) {
                        		$a_evenements[] = $res;
                		}
			}
                	$db->close();

                	echo $_GET['jsoncallback'] . '(' . json_encode($a_evenements) . ');';
        	}

		public static function getListeEvenementsReglage() {
                        header('Content-type: application/json');

                        $db = new TEvenement();
                        $a_result = $db->query("SELECT PRENOM,DESCRIPTION_EVENEMENT,DATE_EVENEMENT,HEURE_EVENEMENT,PERIODE_EVENEMENT
                                                From EVENEMENT AS E JOIN MEMBRE AS M ON E.ID_MEMBRE=M.ID_MEMBRE
                                                WHERE IS_LOGEMENT = 0
                                                ORDER BY DATE_EVENEMENT DESC;"
                                                );

                        $a_evenements = array();
			if(!empty($a_result)) {
                        	while($res = $a_result->fetchArray(SQLITE3_ASSOC)) {
                                	$a_evenements[] = $res;
                        	}
			}
                        $db->close();

                        echo $_GET['jsoncallback'] . '(' . json_encode($a_evenements) . ');';
                }
		public static function addEvent($i_date,$b_isLogement,$s_heureEvent,$s_periode,$s_description,$i_idMembre) {
			$db = new TEvenement();
			error_log("[TEvenement.php] description ==> $s_description \n", 3, "/var/www/application/tacks-php.log");
                	$statement = $db->prepare('INSERT INTO EVENEMENT VALUES (NULL,:date,:heureEvent,:periode,:isLogement,:description,:idMembre)');
                	$statement->bindValue(':date', $i_date,SQLITE3_TEXT);
                	$statement->bindValue(':isLogement', $b_isLogement,SQLITE3_INTEGER);
                	$statement->bindValue(':heureEvent', $s_heureEvent,SQLITE3_TEXT);
                	$statement->bindValue(':periode', $s_periode,SQLITE3_TEXT);
			$statement->bindValue(':description', $s_description,SQLITE3_TEXT);
                	$statement->bindValue(':idMembre', $i_idMembre,SQLITE3_INTEGER);

                	$result = $statement->execute();
                	$db->close();

                	$return["json"] = json_encode($result);
                	echo json_encode($_POST);

		}

	}
?>
